Skip to content

Conversation

williasc
Copy link
Member

@williasc williasc commented Nov 24, 2026 •

Closes #125

One command on the VM updates the site to main: ./scripts/deploy.sh.

What it does

  • Builds the frontend and type-checks the backend before the running site is touched.
  • Backs up the database (without the 21 million X-Wines ratings) and keeps the newest five backups.
  • Copies the backend to /opt/project2/backend, runs the migrations and restarts the systemd service project2-backend. If a migration fails or the backend does not start, it puts the previous code back.
  • Copies the frontend to /var/www/html/project2, and deletes files in assets/ older than 14 days.
  • Checks the site, the API through Apache, and the cache headers.

docs/vm.md is rewritten around the script, from 430 to about 250 lines. npm run buildvm is removed.

Different from the issue: no shared group. A service user project2 runs the backend from /opt/project2, and the script uses sudo for the copy and the restart. Everyone deploys from their own checkout.

How it was tested

In a Docker container with Ubuntu 26.04, systemd, PostgreSQL and Apache 2.4.66, set up with "First-time setup" in docs/vm.md and the Test dataset:

  • First deploy on an empty database, later deploys, and a deploy that updates the script itself
  • Dirty checkout and wrong branch: refuses before doing anything
  • Type error in the frontend or the backend: stops, the site is untouched
  • Broken migration: stops, the previous code is back, the API still answers after a restart
  • Backend that crashes at start: goes back to the previous backend
  • Killed backend process: systemd starts it again
  • Old asset files are deleted after 14 days, five backups are kept, and a restore brings back accounts, reviews, lists and wines

Not tested: the Full dataset, the real sudo password prompt, and a reboot of the VM. npm run check passes.

After merge: the one-time setup on the VM (service user, .env, stop the nohup backend) and the first deploy, done together.

Use of AI

Claude Code wrote the script, the service file and the new docs/vm.md, and ran the tests in the container. The tests found three faults that were then fixed: the first deploy failed on an empty database, an update of the script did not take effect in the same run, and a crashing backend stayed down. I read the script and the docs and decided the design (own checkout, service user, no shared group).

@williasc williasc requested a review from siverne November 24, 2026 13:38
@williasc williasc requested a review from a team as a code owner November 24, 2026 13:38
@williasc williasc linked an issue Nov 24, 2026 that may be closed by this pull request
4 tasks
Copy link
Member

@siverne siverne left a comment •

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read through the script, the service file and the docs. This is a big improvement over the manual steps: the build is checked before the site is touched, the backend rolls back if it does not start, there is a backup before every deploy, and the checks at the end catch a missing Apache config. Running the backend as a systemd service fixes the restart problem from the peer reviews. I checked that vite.config.ts already has base: '/project2/', so dropping --base from the build is fine.

Some things before or right after merge:

  • How was the script tested? It runs with sudo against the live site. #125 says it is done when someone has deployed with it and the backend comes back after sudo systemctl restart. Let's do the first deploy together right after merge.
  • #125 says the manual steps should stay in docs/vm.md as a fallback. They are removed now. A short version would help if the script stops halfway.
  • If a migration or the demo user fails, the new code is already in /opt/project2/backend while the old process keeps running, and the next restart starts the new code on the old schema. Restoring backend.previous when that step fails would avoid it.
  • frontend/README.md still says npm run buildvm.
  • The README says the backend runs as a systemd service, which is only true after the setup on the VM.

@williasc
Copy link
Member Author

Thanks, good points. Pushed fixes for three of them:

  • Migration or demo user fails: the script now puts backend.previous back before it stops, so a restart starts the old code. Tested with a broken migration and a code change in the same commit: the deploy stopped, the old code was back, and the API answered before and after systemctl restart.
  • Manual fallback: docs/vm.md has a new section "If the script stops". It says what state the site is in for each step and what to do, and gives the commands to put the previous backend back by hand. Every step can be repeated, so the normal fix is to run the script again.
  • frontend/README.md: fixed.

How it was tested: in a Docker container with Ubuntu 26.04, systemd, PostgreSQL and Apache 2.4.66 (the VM's version), set up with the steps in "First-time setup". See the description. Not tested: the Full dataset, the real sudo prompt and a reboot. Agreed, let's do the first deploy together right after merge.

README and systemd: true. It is correct once the first deploy is done, so I'd rather do that deploy straight after the merge than write the README in two steps.

@siverne siverne self-requested a review November 24, 2026 13:54
Copy link
Member

@siverne siverne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks good to me now:)

@siverne siverne merged commit efeb718 into main Nov 24, 2026
@siverne siverne deleted the 125-deploy-script-for-the-vm branch November 24, 2026 13:55
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deploy script for the VM
2 participants